package com.centit.exchange.controller;

import com.centit.exchange.po.ExchangeRate;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.centit.exchange.service.ExchangeRateManager;
	

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.centit.framework.common.ResponseMapData;
import com.centit.framework.components.CodeRepositoryUtil;
import com.centit.framework.core.controller.BaseController;
import com.centit.framework.core.dao.CodeBook;
import com.centit.framework.model.basedata.IRoleInfo;
import com.centit.framework.security.model.CentitUserDetails;
import com.centit.support.database.utils.PageDesc;
import com.centit.util.LayuiResultUtil;

/**
 * ExchangeRate  Controller.
 * create by scaffold 2020-04-08 
 * @author 脚手架，改成开发者的名字
 * T_EXCHANGE_RATE汇率表   
*/


@Controller
@RequestMapping("/exchangerate")
public class ExchangeRateController  extends BaseController {
	private static final Log log = LogFactory.getLog(ExchangeRateController.class);
	
	@Resource
	private ExchangeRateManager exchangeRateMag;	
	

    @RequestMapping(value="/getTwoByTwoFbDate/{dateks}/{datejs}",method = RequestMethod.GET)
    @ResponseBody
    public JSONObject getTwoByTwoFbDate(@PathVariable String dateks,@PathVariable String datejs) {
    	JSONObject jsonObject = new JSONObject();
    	ExchangeRate ksExchangeRate = exchangeRateMag.getObjectById(dateks);
    	ExchangeRate jsExchangeRate = exchangeRateMag.getObjectById(datejs);
    	if(ksExchangeRate != null && jsExchangeRate != null) {
    		jsonObject.put("code", "200");
    		jsonObject.put("dateks", ksExchangeRate);
    		jsonObject.put("datejs", jsExchangeRate);
    		jsonObject.put("message", "查询成功");
    	} else {
    		jsonObject.put("code", "500");
    		if(ksExchangeRate == null && jsExchangeRate == null) {
    			jsonObject.put("message", "所选的两个日期的汇率本系统暂未收录！");
    		} else if(ksExchangeRate == null) {
    			jsonObject.put("message", "开始日期的汇率本系统暂未收录！");
    		} else if(jsExchangeRate == null) {
    			jsonObject.put("message", "结束日期的汇率本系统暂未收录！");
    		}
    	}
    	return jsonObject;
    }
    
    @RequestMapping(value="/getMinMax" ,method = RequestMethod.GET)
    @ResponseBody
    public JSONObject list(HttpServletRequest request, HttpServletResponse response) {
    	JSONObject jsonObject = new JSONObject();
    	Map<String, Object> searchColumn = convertSearchColumn(request);
    	searchColumn.put(CodeBook.SELF_ORDER_BY, " FB_DATE ");
    	JSONArray jsonArrayObjects  = exchangeRateMag.listObjectsAsJson( searchColumn, null);
    	jsonObject.put("min", jsonArrayObjects.getJSONObject(0).getString("fbDate"));
    	jsonObject.put("max", jsonArrayObjects.getJSONObject(jsonArrayObjects.size()-1).getString("fbDate"));
		return jsonObject;
    }
    
    @RequestMapping(value="/getUseDate/{date}" ,method = RequestMethod.GET)
    @ResponseBody
    public JSONObject getUseDate(@PathVariable String date,HttpServletRequest request, HttpServletResponse response) {
    	String[] datas = date.split("-");
    	if(Integer.parseInt(datas[1])<10){
    		date = datas[0]+"-0"+datas[1];
    	}else{
    		date = datas[0]+"-"+datas[1];
    	}
    	JSONObject jsonObject  = exchangeRateMag.getUseDate(date);
		return jsonObject;
    }
}
